package com.example.cxx.controller;

import com.example.cxx.Utils.CookieUtil;
import com.example.cxx.pojo.Course;
import com.example.cxx.pojo.Result;
import com.example.cxx.pojo.YuMing;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("course/api/*")
public class CourseController {
    @GetMapping("/course")
    public Result getCourse(String cookie,String classNum) throws IOException {
        if("".equals(cookie)){
            return Result.error("LOGIN-FAIL");
            }
        try {
            //获取当前学期
            Document index=Jsoup.connect("\n" +
                    YuMing.qianzhui+YuMing.yuming+"/EaWeb/Manager/Module/NetEa/Schedule/Query/Default.aspx").cookies(CookieUtil.convertCookie(cookie)).get();
            if(index.getElementById("Account")!=null&&"".equals(index.getElementById("Account").val())){
                return Result.error("LOGIN-FAIL");
            }
            Element optionTerm=index.getElementById("DropDownListXnXq").child(0);
            //-------------------------以上是当前学期信息
            Connection connect = Jsoup.connect(YuMing.qianzhui+YuMing.yuming+"/EaWeb/Manager/Module/NetEa/Schedule/Query/Default.aspx").cookies(CookieUtil.convertCookie(cookie));
            String __VIEWSTATE=connect.get().getElementById("__VIEWSTATE").val();
            String __VIEWSTATEGENERATOR=connect.get().getElementById("__VIEWSTATEGENERATOR").val();
            //添加参数
            connect.data("__EVENTTARGET","")
                    .data("__EVENTARGUMENT","")
                    .data("__LASTFOCUS","")
                    .data("__VIEWSTATE",__VIEWSTATE)
                    .data("__VIEWSTATEGENERATOR",__VIEWSTATEGENERATOR)
                    .data("__VIEWSTATEENCRYPTED","")
                    .data("ctl00$PageBody$XnXq$DropDownListXnXq", optionTerm.text())
                    .data("ctl00$PageBody$RbtlType","班级")
                    .data("ctl00$PageBody$TxbInput",classNum)
                    .data("ctl00$PageBody$BtnShowTable","查看课表")
                    .data("ctl00$PageBody$TxbDate","")
                    .data("ctl00$DoPostBack_LiuManRang: ","");
            Connection.Response response = connect.method(Connection.Method.POST).ignoreContentType(true).followRedirects(true).execute();
            //获取数据，处理成HTML
            Document document = response.parse();
            Element courseTable=document.getElementById("TabSchedule");//课程信息表
            //校验过期
            if(courseTable==null){
                return Result.error("LOGIN-FAIL");
            }
            List<Course> courseList=new ArrayList<>();
            int j=0;
            for (int i=2;i<39;i++){
                Element tableRowItem=document.getElementById("TableCell"+i);
                if(tableRowItem==null||i==9||i==17||i==25||i==33){continue;}
                Course course=new Course(j++,tableRowItem.text().replaceAll(Jsoup.parse("&nbsp;").text(), " "));
                courseList.add(course);
            }
            return Result.success(courseList);
        }
        catch (Exception e){
            return Result.error("LOGIN-FAIL");
        }

    }
}
